package com.tianji.common.autoconfigure.mybatis;

import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import com.tianji.common.utils.ReflectUtils;
import com.tianji.common.utils.UserContext;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.Date;

import static com.tianji.common.constants.Constant.*;

public class MyBatisAutoFillInterceptor implements InnerInterceptor {

    @Override
    public void beforeUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {
        //1.更新操作
        updateExe(parameter);
        //2.插入操作
        insertExe(ms, parameter);
    }

    private void insertExe(MappedStatement ms, Object parameter){
        //1.判断当前操作是否是插入操作
        if(ms.getSqlCommandType().compareTo(SqlCommandType.INSERT) == 0) {
            //2.判断是否有creater字段，如果
            if(ReflectUtils.containField(DATA_FIELD_NAME_CREATER, parameter.getClass())){
                Long userId = UserContext.getUser();
                //3.有userId也存在并设置creater
                if(userId != null){
                    //4.当前操作人设置到创建人字段
                    ReflectUtils.setFieldValue(parameter, DATA_FIELD_NAME_CREATER, userId);
                }
            }
            //判断是否有create_time
            if(ReflectUtils.containField(DATA_FIELD_NAME_CREATE_TIME_CAMEL,parameter.getClass())){
                Object currentTime = getCurrentTime(parameter.getClass(), DATA_FIELD_NAME_CREATE_TIME_CAMEL);
                if (currentTime != null) {
                    ReflectUtils.setFieldValue(parameter, DATA_FIELD_NAME_CREATE_TIME_CAMEL, currentTime);
                }
            }

        }
    }

    private void updateExe(Object parameter){
        //1.判断是否有updater字段
        if(ReflectUtils.containField(DATA_FIELD_NAME_UPDATER, parameter.getClass())){
            Long userId = UserContext.getUser();
            //2.如果有userId也存在并设置updater
            if(userId != null){
                //3.当前用户设置到更新人字段
                ReflectUtils.setFieldValue(parameter, DATA_FIELD_NAME_UPDATER, userId);
            }
        }
        //判断是否有update_time
        if(ReflectUtils.containField(DATA_FIELD_NAME_UPDATE_TIME_CAMEL,parameter.getClass())){
            Object currentTime = getCurrentTime(parameter.getClass(), DATA_FIELD_NAME_UPDATE_TIME_CAMEL);
            if (currentTime != null) {
                ReflectUtils.setFieldValue(parameter, DATA_FIELD_NAME_UPDATE_TIME_CAMEL, currentTime);
            }
        }
    }

    // 根据字段类型获取当前时间
    private Object getCurrentTime(Class<?> clazz, String fieldName)  {

        Class<?> fieldType = null;
        try {
            fieldType = clazz.getDeclaredField(fieldName).getType();
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
        if (fieldType == LocalDateTime.class) {
            return LocalDateTime.now();
        } else if (fieldType == Date.class) {
            return new Date();
        }
        // 支持其他时间类型，如Timestamp等
        return null;
    }
}
